package gui.window;

import gui.dialogs.About;
import gui.dialogs.Connect;
import gui.frames.Profiles;
import gui.panels.Customizer;
import gui.panels.LeftMenu;
import gui.panels.LogsArea;
import gui.utilities.LogsHandler;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.SSHConnectionProxy;
import net.miginfocom.swing.MigLayout;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

	private JFrame frmRouterManager;
	private LeftMenu leftMenuPane;
	private LogsArea logsAreaPane;
	private Customizer customizerPane;
	private Image icon;
	private Logger logger;

	public LeftMenu getLeftMenuPane() {
		return leftMenuPane;
	}

	public LogsArea getLogsAreaPane() {
		return logsAreaPane;
	}

	public Customizer getCustomizerPane() {
		return customizerPane;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frmRouterManager.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		icon = Toolkit.getDefaultToolkit().getImage("resources/icon.png");
		logger = LoggerFactory.getLogger(Main.class);
		frmRouterManager = new JFrame();
		frmRouterManager.setTitle("Router Manager");
		frmRouterManager.setIconImage(icon);
		frmRouterManager.setBounds(100, 100, 956, 667);
		frmRouterManager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frmRouterManager.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmConnect = new JMenuItem("Connect");
		mntmConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Connect connectDialog = new Connect();
				connectDialog.setTitle("Connect");
				connectDialog.setIconImage(icon);
				connectDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				connectDialog.pack();
				connectDialog.setLocationRelativeTo(frmRouterManager.getRootPane());
				connectDialog.setVisible(true);
			}
		});
		mnFile.add(mntmConnect);
		
		JMenuItem mntmDisconnect = new JMenuItem("Disconnect");
		mntmDisconnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!SSHConnectionProxy.isConnected()) 
						return;	
					SSHConnectionProxy.close();
					logger.info("You've been disconnected");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		mnFile.add(mntmDisconnect);
		
		JSeparator separator = new JSeparator();
		mnFile.add(separator);
		
		JMenuItem mntmProfiles = new JMenuItem("Profiles");
		mntmProfiles.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Profiles profilesFrame = new Profiles();
				profilesFrame.setTitle("Profiles");
				profilesFrame.setIconImage(icon);
				profilesFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				profilesFrame.pack();
				profilesFrame.setLocationRelativeTo(frmRouterManager.getRootPane());
				profilesFrame.setVisible(true);
			}
		});
		mnFile.add(mntmProfiles);
		
		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);
		
		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				About aboutDialog = new About();
				aboutDialog.setTitle("About");
				aboutDialog.setIconImage(icon);
				aboutDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				aboutDialog.pack();
				aboutDialog.setLocationRelativeTo(frmRouterManager.getRootPane());
				aboutDialog.setVisible(true);
			}
		});
		mnHelp.add(mntmAbout);
		
		Container contentPane = frmRouterManager.getContentPane();
		contentPane.setLayout(new MigLayout("fill", "[150px:150px:150px,center][300px:n,fill]", "[300:n,grow][100px:250px]"));
		
		customizerPane = new Customizer();
		contentPane.add(customizerPane, "cell 1 0,grow");
		
		leftMenuPane = new LeftMenu();
		leftMenuPane.setCustomizer(customizerPane);
		contentPane.add(leftMenuPane, "cell 0 0,growy");
		
		logsAreaPane = new LogsArea();
		contentPane.add(logsAreaPane, "cell 0 1 2 1,grow");
		setupJdkLoggerHandler(logsAreaPane);
	}
	
	private static void setupJdkLoggerHandler(LogsArea logsArea) {
		LogsHandler.setLogsArea(logsArea);
		StringBuffer buf = new StringBuffer();
		buf.append("handlers = gui.utilities.LogsHandler, java.util.logging.ConsoleHandler");
		buf.append("\n");
		buf.append(".level = INFO");
		buf.append("\n");
		buf.append("gui.utilities.LogsHandler.level = INFO");
		buf.append("\n");
		buf.append("java.util.logging.ConsoleHandler.level = INFO");
		buf.append("\n");
		try {
			java.util.logging.LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(buf.toString().getBytes()));
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
